package com.sz.app.customer.api;

import com.sz.biz.app.web.BaseController;
import com.sz.app.customer.exception.ErrorCodes;
import com.sz.biz.common.customer.dto.CusCompanyDto;
import com.sz.biz.common.customer.dto.CusEnterpriseInfoDto;
import com.sz.biz.common.customer.entity.CusCustomer;
import com.sz.biz.common.customer.entity.CusUser;
import com.sz.biz.common.customer.service.CustomerCompanyService;
import com.sz.biz.common.customer.service.CustomerService;
import com.sz.biz.common.customer.service.CustomerUserService;
import com.sz.biz.pay.account.service.AccountService;
import com.sz.biz.pay.account.service.ThirdPayAccountService;
import com.sz.common.base.dto.QResultDto;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.core.service.PrincipalUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xutao on 17/6/2.
 */
/**
 * Created by xutao on 17/6/1.
 */
@RestController
@RequestMapping("${api}/user/authentication")
@Api(description = " ", tags = "003、账户-认证信息")
public class AuthenticationController extends BaseController {

    @Autowired
    CustomerService customerService;

    @Autowired
    CustomerCompanyService customerCompanyService;

    @Autowired
    CustomerUserService customerUserService;

    @Autowired
    AccountService accountService;

    @Autowired
    ThirdPayAccountService thirdPayAccountService;

    /**
     * 提交企业认证信息
     *
     * @throws Exception ex
     */
    @ApiOperation(value = "提交企业认证信息", notes = "提交企业认证信息")
    @RequestMapping(value = "/companyAuthentication", method = {RequestMethod.PUT})
    public ResultDto companyAuthentication(@RequestBody CusEnterpriseInfoDto cusEnterpriseInfoDto) throws Exception {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            throw Exceptions.bizException(ErrorCodes.ERROR_CAN_NOT_UPDATE_COMPANY_AUTH_MESSAGE);
        }
        CusUser cusUser = null;
        try {
            Integer cusUserId = PrincipalUtils.getAccountId();
            if (cusUserId > 0) {
                cusUser = customerUserService.findById(cusUserId);
            }
        } catch (Exception ex) {
            throw Exceptions.bizException(ErrorCodes.ERROR_GET_USER_INFO_ERR);
        }
        if (cusUser == null) {
            throw Exceptions.bizException(ErrorCodes.ERROR_GET_USER_INFO_ERR);
        }
        CusCustomer currentCustomer = customerService.findCustomerById(cusUser.getRefCustomerId());
        if (currentCustomer != null && currentCustomer.getAuthStatus() != null && currentCustomer.getAuthStatus() > 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_CAN_NOT_UPDATE_COMPANY_AUTH_MESSAGE);
        }

        CusCompanyDto cusCompanyDto = new CusCompanyDto();
        BeanUtils.copyProperties(cusEnterpriseInfoDto, cusCompanyDto);
        cusCompanyDto.setRefCustomerId(cusUser.getRefCustomerId());
        CusCustomer customer = customerService.findCustomerById(cusUser.getRefCustomerId());
        CusCompanyDto temCompanyDto = customerCompanyService.findCompanyByCustomerId(cusUser.getRefCustomerId());

        if (temCompanyDto != null) {
            cusCompanyDto.setId(temCompanyDto.getId());
        }
        cusCompanyDto.setCreator(customer.getUserName() + " " + customer.getCode());
        List<CusCustomer> cusCustomerList = new ArrayList<CusCustomer>();
        CusCustomer cusCustomer = new CusCustomer();
        cusCustomer.setId(cusUser.getRefCustomerId());
        BeanUtils.copyProperties(customer,cusCustomer);
        cusCustomer.setTradingCurrency(cusEnterpriseInfoDto.getTradingCurrency());
        cusCustomer.setEmail(cusEnterpriseInfoDto.getEmail());
        cusCustomer.setRefSettlementId(cusEnterpriseInfoDto.getRefSettlementId());
        cusCustomer.setAuthStatus(1);
        cusCustomer.setCode(cusUser.getUserName());
        cusCustomerList.add(cusCustomer);
        //修改之前创建的账户信息
        customerService.update(cusCustomerList);
        customerService.createPayAccount(cusCustomer,cusCompanyDto);
        int companyId = customerCompanyService.updateCompanyById(cusCompanyDto);
        customerUserService.updateEmailById(cusUser.getId(),cusEnterpriseInfoDto.getEmail());
        ResultDto dto = new ResultDto();
        dto.setMsg("企业认证信息提交成功");
        dto.setData(companyId);
        return dto;
    }

    /**
     * 凭借账户ID查找企业信息
     */
    @ApiOperation(value = "凭借账户ID查找企业信息", notes = "凭借账户ID查找企业信息")
    @RequestMapping(value = "/enterpriseinfo", method = {RequestMethod.GET})
    public QResultDto findEnterpriseInfoById() {

        QResultDto qResultDto = new QResultDto();

        CusUser cusUser = customerUserService.findById(PrincipalUtils.getAccountId());

        if (cusUser != null) {

            CusEnterpriseInfoDto cusEnterpriseInfoDto = customerService.findEnterpriseInfoById(cusUser.getRefCustomerId());

            qResultDto.setData(cusEnterpriseInfoDto);
        } else {

            qResultDto.setErrorCode(-1);
        }

        return qResultDto;
    }

    @ApiOperation(value = "获取企业认证信息", notes = "获取企业认证信息")
    @RequestMapping(value = "/getcompany", method = RequestMethod.GET)
    public ResultDto getCompanyAuthentication() {
        ResultDto resultDto = new ResultDto();
        CusUser cusUser = null;
        try {
            Integer cusUserId = PrincipalUtils.getAccountId();
            if (cusUserId > 0) {
                cusUser = customerUserService.findById(cusUserId);
            }
        } catch (Exception ex) {
            throw Exceptions.bizException(ErrorCodes.ERROR_GET_USER_INFO_ERR);
        }
        if (cusUser == null) {
            throw Exceptions.bizException(ErrorCodes.ERROR_GET_USER_INFO_ERR);
        }
        CusEnterpriseInfoDto cusEnterpriseInfoDto = customerService.findEnterpriseInfo();
        resultDto.setData(cusEnterpriseInfoDto);
        return resultDto;
    }


    @ApiOperation(value = "检查是否认证通过", notes = "检查企业是否认证通过")
    @RequestMapping(value = "/checkauthstatus", method = {RequestMethod.GET})
    public ResultDto checkAuthStatus() {
        ResultDto resultDto = new ResultDto();
        if(SecurityUtils.getSubject().isAuthenticated()){
            resultDto.setData(2);
            return resultDto;
        }
        CusUser cusUser = null;
        try {
            Integer cusUserId = PrincipalUtils.getAccountId();
            if (cusUserId > 0) {
                cusUser = customerUserService.findById(cusUserId);
            }
        } catch (Exception ex) {
            throw Exceptions.bizException(ErrorCodes.ERROR_GET_USER_INFO_ERR);
        }
        if (cusUser == null) {
            throw Exceptions.bizException(ErrorCodes.ERROR_GET_USER_INFO_ERR);
        }
        int result = customerService.checkCustomerAuthStatus(cusUser.getRefCustomerId());
        resultDto.setData(result);
        return resultDto;
    }
}
